package com.ziiwee.reduce.describe.creators.handlers;

import com.ziiwee.reduce.commons.utils.StringUtil;
import com.ziiwee.reduce.describe.FieldDescribe;
import com.ziiwee.reduce.describe.HttpRequestParameterType;
import com.ziiwee.reduce.describe.ResourceMethod;
import com.ziiwee.reduce.describe.models.FieldDescribeModel;
import org.springframework.core.ResolvableType;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ziiwee
 * @data 2020/8/12
 */
public class RestUpdateHandlerDescribeCreator extends AbstractRestHandlerCreator {

    private final Class<?> entityClass;
    private final Class<?> idType;
    private final String basePattern;
    public RestUpdateHandlerDescribeCreator(Class<?> entityClass, Class<?> idType, String basePattern) {
        this.entityClass = entityClass;
        this.idType = idType;
        this.basePattern = basePattern;
    }

    @Override
    public ResourceMethod getResourceMethod() {
        return ResourceMethod.UPDATE;
    }

    @Override
    public Class<?> getEntityClass() {
        return entityClass;
    }

    @Override
    public String getBasePattern() {
        return basePattern;
    }

    @Override
    public Map<HttpRequestParameterType, List<FieldDescribe>> getParameters() {
        FieldDescribe id = getIdProperty(ResolvableType.forClass(idType));
        String name = StringUtil.firstToLowerCase(entityClass.getSimpleName());
        FieldDescribe entity = new FieldDescribeModel(name, ResolvableType.forClass(entityClass), name, "", false, true, true, null, null);

        Map<HttpRequestParameterType, List<FieldDescribe>> map = new HashMap<>();
        map.put(HttpRequestParameterType.PATH, Collections.singletonList(id));
        map.put(HttpRequestParameterType.BODY, Collections.singletonList(entity));
        return map;
    }
}
